//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    wTitle:"36.5℃",
    wWe:"晴朗",
    dTemp:"风向",
    eTemp:"风力",
    wTemp:"湿度",
    tTemp:"空气指数",
    todayDate:"",
    todayTempScop:""
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log("Index_onLoad")
    wx.request({
      url: 'http://apis.juhe.cn/simpleWeather/query',
      data:{
        key:"d0906d3763a3fec91d47b8787c6acf45",
        city:"西安"
      },
      success: res => {
        console.log(JSON.stringify(res.data.result))
        this.getNow(res)
      },
      fail(res) {
        console.log("请求失败！")
      }
    })
    //获取当前时间
    this.setData({
      todayDate: util.formatTime(new Date())
    });

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  getNow(res) {
    let data = res.data.result.future.date
    //温度
    let temperature = res.data.result.realtime.temperature
    //湿度
    let humidity = res.data.result.realtime.humidity
    //天气情况
    let info = res.data.result.realtime.info
    //风向
    let direct = res.data.result.realtime.direct
    //风力
    let power = res.data.result.realtime.power
    //空气指数
    let aqi = res.data.result.realtime.aqi
    this.setData({
      wTitle: temperature + "℃",
      wWe: info,
      dTemp: direct,
      eTemp: power,
      wTemp: humidity,
      tTemp: aqi
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShow: function () {
    console.log("Index_Show")
  },
  onHide: function () {
    console.log("Index_Hide")
  },
  getUserInfo: function (e) {

  }
})
